<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>广告添加</title>
    <link rel="stylesheet" type="text/css" href="../static/layui/css/layui.css">
    <script type="text/javascript" src="../static/js/jquery-3.3.1.min.js"></script>
    <script src="../static/layui/layui.js"></script>
</head>
<body>
<form class="layui-form" action="" onsubmit="return false">
    <div class="layui-upload">
        <label class="layui-form-label"></label>
        <button type="button" class="layui-btn" id="test1">上传图片</button>
        <div class="layui-upload-list" id="tupian">
            <img class="layui-upload-img" id="demo1" width="100px" height="100px" style="margin-left: 110px">
            <p id="demoText"></p>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">链接地址:</label>
        <div class="layui-input-inline">
            <input type="text" name="bannerPath" id="bannerPath" lay-verify="required" placeholder="请输入外部链接"
                   autocomplete="off" class="layui-input" maxlength="20">
        </div>
        <span id="nameSpan"></span>
    </div>

    <!--排序-->
    <div class="layui-form-item">
        <label class="layui-form-label">排序:</label>
        <div class="layui-input-inline">
            <input type="number" name="bannerSort" id="bannerSort" lay-verify="required" placeholder="请输入依次顺序"
                   autocomplete="off" class="layui-input" maxlength="3">
        </div>
        <span id="ageSpan"></span>
    </div>
    <button class="layui-btn layui-btn-primary layui-border-orange" id="button" onclick="sub()" style="margin-left: 110px">提交</button>
<!--    <button class="layui-btn layui-btn-primary layui-border-orange" id="button2" onclick="subs()" style="margin-left: 110px">提交</button>-->
    <button class="layui-btn layui-btn-primary layui-border-red" id="button1" onclick="fh()">返回</button>
</form>
<script>
    layui.use(['upload', 'element', 'layer'], function () {
        var $ = layui.jquery
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer;

        //常规使用 - 普通图片上传
        let uploadInst = upload.render({
            elem: '#test1',
            url: '/imgUpload' //改成您自己的上传接口
            ,
            accept: 'file' //普通文件
            ,
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });

                element.progress('demo', '0%'); //进度条复位
                layer.msg('上传中', {
                    icon: 16,
                    time: 0
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                //……
                $('#demoText').html(''); //置空上传失败的状态
            },
            error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
            //进度条
            ,
            progress: function (n, index, e) {
                element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                if (n == 100) {
                    layer.msg('上传完毕', {
                        icon: 1
                    });
                }
            }
        });
    })
</script>
<script>
    $("#button2").css("display","none");
    //获取上个页面拼接的参数
    var path =  window.location.search;
    //数组获取等号右边数
    var array = path.split("=");
    $(function () {
        if (array[1]!=null){
            $.ajax({
                url: '/selBannersById?id='+array[1]+'',
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    var item = data.data;
                    console.log(data);
                    var box = document.getElementById("tupian");
                    for (var i = 0; i < item.length; i++) {
                        $("#test1").css("display","none");
                        $("#demo1").css("display","none");
                        // $('#button').css("display","none");
                        box.innerHTML=box.innerHTML+'<img class="layui-upload-img" src="' + item[i].bannerUrl +'" id="demo1" width="100px" height="100px" >';
                        $("#bannerPath").val(item[i].bannerPath);
                        $("#bannerSort").val(item[i].bannerSort);
                    }

                }
            })
        }
    });
    //提交
    function sub() {
        let bannerPath = document.getElementById("bannerPath").value;
        let bannerSort = document.getElementById("bannerSort").value;
        if (array[1]==null){
            var flag = confirm("您确定要添加图片吗？");
            if (flag) {
                $.ajax({
                    url: '/addBanners',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        bannerPath: bannerPath,
                        bannerSort: bannerSort
                    },
                    success: function (data) {
                        if (data == 200) {
                            alert("添加成功");
                            window.location.href = "/banners";
                        } else {
                            alert("添加失败")
                        }
                    },
                    error: function () {
                        layer.alert("添加失败", {
                            icon: 7,
                            title: '提示',
                            time: 1000
                        })
                    }
                })
            }
        }else {
            var flag1 = confirm("您确定要修改信息吗？");
            if (flag1) {
                $.ajax({
                    url: '/updIbtu',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        bannerId:array[1],
                        bannerPath: bannerPath,
                        bannerSort: bannerSort
                    },
                    success: function (data) {
                        if (data == 200) {
                            alert("修改成功");
                            window.location.href = "/banners";
                        } else {
                            alert("修改失败")
                        }
                    },
                    error: function () {
                        layer.alert("添加失败", {
                            icon: 7,
                            title: '提示',
                            time: 1000
                        })
                    }
                })
            }
        }

    }

    //返回
    function fh() {
        window.location.href = "/banners";
    }
</script>
</body>
</html>